﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CasanovaStarterGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        string f_name;

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            using (System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog())
            {
                dlg.Filter = "fs (*.fs)|*.fs|Tutti i file (*.*)|*.*";
                dlg.Multiselect = false;

                System.Windows.Forms.DialogResult result = dlg.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    f_name = dlg.FileName;
                    if (System.IO.File.Exists(f_name))
                    {
                        Console.WriteLine("Path changed and saved succesfully");
                    }
                }
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            StartProcess("fsc.exe \"" + f_name + "\"", true);
        }

        private void StartProcess(string name, bool redirect = true)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C " + name;
            process.StartInfo = startInfo;
            if (redirect)
            {
                RedirectOutput(process);
            }
            process.Start();
            if (redirect)
            {
                process.BeginOutputReadLine();
            }
        }
        private void RedirectOutput(Process myProcess)
        {
            // This needs to be set to false, in order to actually redirect the standard shell output
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.RedirectStandardOutput = true;

            // This is the event that is triggered when output data is received.
            // I changed this to Console.WriteLine() - you can use whatever you want basically...
            myProcess.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data);

            //myProcess.Start();

            //myProcess.BeginOutputReadLine(); // without this, the OutputDataReceived event won't ever be triggered
        }
    }
}